<template>
    <div class="app-container goodsall">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
            <el-form-item label="商品品牌" prop="brandName">
                <el-select class="select" v-model="queryParams.brandName" @change="handleQuery" allow-create filterable default-first-option placeholder="请选择品牌标签">
                    <el-option v-for="item in brandOptions" :key="item.brandNo" :label="item.brandName" :value="item.brandName"> </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="品类" prop="categoryNo">
                <el-cascader v-model="queryParams.categoryNo" :options="options" :props="optionProps" :show-all-levels="false" @change="handleQuery" clearable></el-cascader>
            </el-form-item>
            <el-form-item label="SPU编码" prop="spuNo">
                <el-input v-model="queryParams.spuNo" onkeyup="this.value=this.value.replace(/[^A-Z0-9\-]/g,'');" placeholder="请输入SPU编码" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="SKU编码" prop="skuNo">
                <el-input v-model="queryParams.skuNo" onkeyup="this.value=this.value.replace(/[^A-Z0-9\-]/g,'');" placeholder="请输入SKU编码" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="商品名称" prop="goodsName">
                <el-input v-model="queryParams.goodsName" onkeyup="this.value=this.value.replace(/[[+]/g,'').replace(/[\]+]/g,'');" placeholder="请输入商品名称" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="商品货号" prop="goodsModel">
                <el-input v-model="queryParams.goodsModel" onkeyup="this.value=this.value.replace(/[^A-Z0-9\-]/g,'');" placeholder="请输入商品货号" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="价格" prop="priceStart">
                <el-input v-model="queryParams.priceStart" onkeyup="this.value=this.value.replace(/[^\d]/g,'');" placeholder="请输入价格" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <p class="xian"></p>
            <el-form-item prop="priceEnd">
                <el-input v-model="queryParams.priceEnd" onkeyup="this.value=this.value.replace(/[^\d]/g,'');" placeholder="请输入价格" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5">
                <el-button
                    type="warning"
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['biz:shop:export']"
                    >导出</el-button
                >
            </el-col> -->

            <el-col :span="1.5">
                <el-button :icon="priceIcon" size="mini" @click="resetMoney">价格</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button :icon="saleIcon" size="mini" @click="resetNumber">销量</el-button>
            </el-col>

            <el-radio-group v-model="queryParams.wareHouseType" class="radio" size="medium" style="float: right;" @change="handleQuery">
                <el-radio-button label="国内现货"></el-radio-button>
                <el-radio-button label="欧洲现货"></el-radio-button>
                <el-radio-button label="香港现货"></el-radio-button>
            </el-radio-group>
        </el-row>

        <!-- <el-table
            v-loading="loading"
            :data="goodsList"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="状态  0 正常 1不正常" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="uid" />-->
        <!-- <el-table-column label="品牌名称" align="center" prop="brandName" />
            <el-table-column label="商品货号" align="center" prop="brandNo" />
            <el-table-column label="商品名称" align="center" prop="goodsName" />
            <el-table-column label="spu编号" align="center" prop="goodsNo" />
            <el-table-column label="sku编号" align="center" prop="skuNo" />
            <el-table-column label="总库存" align="center" prop="totalStock" />
            <el-table-column
                label="一般贸易运费"
                align="center"
                prop="tradePriceFreight"
            />
            <el-table-column
                label="一般贸易税"
                align="center"
                prop="tradePriceTax"
            />
            <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
            >
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['biz:shop:edit']"
                        >详情</el-button
                    >
                    <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:shop:remove']"
          >删除</el-button> -->
        <!-- </template>
            </el-table-column>
        </el-table> -->

        <el-table :data="goodsList" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="30"> </el-table-column>
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" inline class="demo-table-expand">
                        <el-form-item class="title" label="等级分类" style="display: block;"> </el-form-item>
                        <el-form-item label="一级分类" class="title_b">
                            <span>{{ props.row.categoryName1 }}</span>
                        </el-form-item>
                        <el-form-item label="二级分类" class="title_b">
                            <span>{{ props.row.categoryName2 }}</span>
                        </el-form-item>
                        <el-form-item label="三级分类" class="title_b">
                            <span>{{ props.row.categoryName3 }}</span>
                        </el-form-item>
                        <el-form-item class="title" label="价格" style="display: block;"> </el-form-item>
                        <el-form-item label="商品采购价" class="title_b">
                            <span>{{ props.row.purchasePrice }}</span>
                        </el-form-item>
                        <el-form-item label="市场参考价" class="title_b">
                            <span>{{ props.row.marketPrice }}</span>
                        </el-form-item>
                        <el-form-item label="欧洲专柜价" class="title_b">
                            <span>{{ props.row.euSalesPrice }}</span>
                        </el-form-item>
                        <el-form-item label="一般贸易税" class="title_b">
                            <span>{{ props.row.tradePriceTax }}</span>
                        </el-form-item>
                        <el-form-item label="跨境税" class="title_b">
                            <span>{{ props.row.crossBorderTax }}</span>
                        </el-form-item>
                        <el-form-item label="一般贸易运费" class="title_b">
                            <span>{{ props.row.tradePriceFreight }}</span>
                        </el-form-item>
                        <el-form-item label="跨境运费" class="title_b">
                            <span>{{ props.row.crossBorderFreight }}</span>
                        </el-form-item>
                        <el-form-item label="香港取货运费" class="title_b">
                            <span>{{ props.row.hkDeliveryPrice }}</span>
                        </el-form-item>
                        <el-form-item class="title" label="库存" style="display: block;"> </el-form-item>
                        <el-form-item label="国内现货数" class="title_b">
                            <span>{{ props.row.domesticStock }}</span>
                        </el-form-item>
                        <el-form-item label="香港现货数" class="title_b">
                            <span>{{ props.row.hkStock }}</span>
                        </el-form-item>
                        <el-form-item label="欧洲现货数" class="title_b">
                            <span>{{ props.row.euStock }}</span>
                        </el-form-item>
                        <el-form-item label="总库存数" class="title_b">
                            <span>{{ props.row.totalStock }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column label="图片" width="130">
                <template slot-scope="scope">
                    <el-image style="width: 100px; height: 100px" :src="scope.row.mainImgUrl" :preview-src-list="scope.row.detailImgSet"> </el-image>

                    <!--                    <img :src="scope.row.mainImgUrl" width="40" height="40" />-->
                    <!-- <img :src="z" width="40" height="40" /> -->
                </template>
            </el-table-column>
            <el-table-column label="品牌" prop="brandName"></el-table-column>
            <el-table-column label="名称" prop="goodsName"> </el-table-column>
            <el-table-column label="货号" prop="goodsModel"> </el-table-column>
            <el-table-column label="SPU编码" prop="goodsNo"> </el-table-column>
            <el-table-column label="SKU编码" prop="skuNo"> </el-table-column>
            <el-table-column label="规格" prop="spec"> </el-table-column>
            <!-- <el-table-column label="已同步的店铺" prop="is_sync"> -->
            <!-- </el-table-column> -->
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <!-- <img :src="scope.row.mainImgUrl" width="40" height="40" /> -->
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">详情</el-button>
                    <!-- <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="lockGoods(scope.row)"
                        >锁定库存</el-button
                    >

                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleDelete(scope.row)"
                        >移除</el-button
                    > -->
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改店铺对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
            <el-form ref="form" :model="form" label-width="100px">
                <div class="flex">
                    <el-form-item label="品牌名称" prop="brandName">
                        <el-input v-model="form.brandName" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="商品名称" prop="goodsName">
                        <el-input v-model="form.goodsName" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="商品货号" prop="brandNo">
                        <el-input v-model="form.goodsModel" :disabled="readonly" />
                    </el-form-item>
                </div>
                <div class="flex">
                    <el-form-item label="一级分类" prop="brandName">
                        <el-input v-model="form.categoryName1" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="二级分类" prop="goodsName">
                        <el-input v-model="form.categoryName2" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="三级分类" prop="brandNo">
                        <el-input v-model="form.categoryName3" :disabled="readonly" />
                    </el-form-item>
                </div>

                <div class="flex">
                    <el-form-item label="产地" prop="brandName">
                        <el-input v-model="form.areaRaw" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="规格" prop="totalStock">
                        <el-input v-model="form.spec" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="材质" prop="goodsName"
                        ><!--no-->
                        <el-input v-model="form.materialRaw" :disabled="readonly" />
                    </el-form-item>
                </div>

                <div class="flex">
                    <el-form-item label="商品采购价" prop="goodsName">
                        <el-input v-model="form.purchasePrice" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="市场参考价" prop="brandNo">
                        <el-input v-model="form.marketPrice" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="建议零售价" prop="totalStock">
                        <el-input v-model="form.retailPrice" :disabled="readonly" />
                    </el-form-item>

                    <!--                    <el-form-item label="建议零售价" prop="brandNo">-->
                    <!--                        <el-input v-model="form.marketPrice" :disabled="readonly" />-->
                    <!--                    </el-form-item>-->
                </div>
                <div class="flex">
                    <el-form-item label="欧洲专柜价" prop="brandName">
                        <el-input v-model="form.euSalesPrice" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="香港交货价" prop="brandName">
                        <el-input v-model="form.hkDeliveryPrice" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="spu编号" prop="goodsNo">
                        <el-input v-model="form.goodsNo" :disabled="readonly" />
                    </el-form-item>

                    <!--                    <el-form-item label="一般贸易价" prop="goodsName">-->
                    <!--                        <el-input v-model="form.categoryName1" :disabled="readonly" />-->
                    <!--                    </el-form-item>-->
                </div>
                <div class="flex">
                    <el-form-item label="sku编号" prop="skuNo">
                        <el-input v-model="form.skuNo" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="一般贸易运费" prop="tradePriceFreight">
                        <el-input v-model="form.tradePriceFreight" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="一般贸易税" prop="tradePriceTax">
                        <el-input v-model="form.tradePriceTax" :disabled="readonly" />
                    </el-form-item>
                </div>

                <div class="flex">
                    <el-form-item label="跨境BC运费" prop="brandNo">
                        <el-input v-model="form.crossBorderFreight" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="跨境BC税" prop="brandNo">
                        <el-input v-model="form.crossBorderTax" :disabled="readonly" />
                    </el-form-item>

                    <el-form-item label="总库存" prop="totalStock">
                        <el-input v-model="form.totalStock" :disabled="readonly" />
                    </el-form-item>
                </div>
                <div class="flex">
                    <el-form-item label="国内现货数" prop="totalStock">
                        <el-input v-model="form.domesticStock" :disabled="readonly" />
                    </el-form-item>
                    <el-form-item label="香港现货数" prop="skuNo">
                        <el-input v-model="form.hkStock" :disabled="readonly" />
                    </el-form-item>

                    <el-form-item label="欧洲现货数" prop="totalStock">
                        <el-input v-model="form.euStock" :disabled="readonly" />
                    </el-form-item>
                </div>
            </el-form>
            <div class="goodImages">
                <p class="imgTitle">图片</p>
                <p v-if="form.detailImgSet !== undefined && form.detailImgSet.length == 0">暂无图片</p>
                <img v-for="(item, index) in form.detailImgSet" class="goodImg" :src="item" alt="" :v-key="index" />
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listGoods, getGoods, exportGoods } from '@/api/biz/goods';

export default {
    name: 'Shop',
    data() {
        return {
            priceIcon: 'el-icon-top',
            saleIcon: 'el-icon-top',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            readonly: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 店铺表格数据
            goodsList: [],
            // 弹出层标题
            title: '',
            // 状态字典
            statusOptions: [],
            maoyiOptions: [],
            platOptions: [],
            catogeryOptions: [
                {
                    name: '选项一',
                    id: 1
                },
                {
                    name: '选项二',
                    id: 2
                }
            ],
            brandOptions: [
                {
                    name: '选项一',
                    id: 1
                },
                {
                    name: '选项二',
                    id: 2
                }
            ],
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNo: 1,
                pageSize: 10,
                spuNo: undefined,
                skuNo: undefined,
                goodsName: undefined,
                brandName: undefined,
                goodsModel: undefined,
                categoryNo: undefined,
                priceStart: undefined,
                priceEnd: undefined,
                priceSort: undefined,
                salesSort: undefined,
                wareHouseType: undefined
            },
            // 表单参数
            form: {},
            // 表单校验

            rules: {},
            radio: '国内现货', // 货源
            // 按钮组
            drawer: false,
            direction: 'ttb',
            selectCategory: [],
            optionProps: {
                value: 'id',
                label: 'name',
                children: 'children',
                multiple: false,
                emitPath: false
            },

            options: [],
            brandOptions: [],
            z: require('../../../assets/image/profile.jpg'),
            srcList: []
        };
    },
    created() {
        this.getUrl('/web/getCommonData').then(response => {
            this.options = this.getTreeData(response.data.catogery);
            this.brandOptions = response.data.brand;
            //   this.maoyiOptions = response.data.maoyi;
        });
        this.getList();
    },
    methods: {
        resetMoney() {
            if (this.priceIcon == 'el-icon-bottom') {
                this.priceIcon = 'el-icon-top';
                this.queryParams.priceSort = 2;
            } else {
                this.priceIcon = 'el-icon-bottom';
                this.queryParams.priceSort = 1;
            }
            this.getList();
        },
        resetNumber() {
            if (this.saleIcon == 'el-icon-bottom') {
                this.saleIcon = 'el-icon-top';
                this.queryParams.salesSort = 2;
            } else {
                this.saleIcon = 'el-icon-bottom';
                this.queryParams.salesSort = 1;
            }
            this.getList();
        },

        getTreeData(data) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].children.length < 1) {
                    data[i].children = undefined;
                } else {
                    this.getTreeData(data[i].children);
                }
            }
            return data;
        },
        /** 查询店铺列表 */
        getList() {
            this.loading = false;

            if (this.queryParams.wareHouseType == '国内现货') {
                this.queryParams.wareHouseType = 2;
            }
            if (this.queryParams.wareHouseType == '欧洲现货') {
                this.queryParams.wareHouseType = 1;
            }
            if (this.queryParams.wareHouseType == '香港现货') {
                this.queryParams.wareHouseType = 3;
            }

            listGoods(this.queryParams, 0)
                .then(response => {
                    var resData = response.data;
                    if (resData.code == 0) {
                        this.goodsList = resData.content;
                        this.total = resData.total;
                        this.goodsList.forEach(element => {
                            element.detailImgSet = element.detailImgSet.split(',');
                            element.detailImgSet.push(element.mainImgUrl);
                            //console.log(element.detailImgSet);
                        });
                    }

                    this.loading = false;
                })
                .catch(e => console.log(e)); //捕获异常;
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                uid: undefined,
                plat: undefined,
                name: undefined,
                appKey: undefined,
                appSecret: undefined,
                tradefrom: undefined,
                jycategory: [],
                jypipai: [],
                createdTime: undefined,
                status: '0'
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            if (this.queryParams.wareHouseType == '国内现货') {
                this.queryParams.wareHouseType = 2;
            }
            if (this.queryParams.wareHouseType == '欧洲现货') {
                this.queryParams.wareHouseType = 1;
            }
            if (this.queryParams.wareHouseType == '香港现货') {
                this.queryParams.wareHouseType = 3;
            }
            this.queryParams.pageNo = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams.wareHouseType = 0;
            this.queryParams.priceSort = 0;
            this.queryParams.salesSort = 0;
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.skuNo);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = '添加店铺';
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const skuNo = row.skuNo;
            getGoods(skuNo).then(response => {
                var resData = response.data;
                if (resData.code == 0) {
                    this.form = resData.content;
                    if (this.form.detailImgSet != null) {
                        this.form.detailImgSet = this.form.detailImgSet.split(',');
                        //this.form.detailImgSet.push(this.form.mainImgUrl);
                    } else {
                        this.form.detailImgSet = [];
                        //this.form.detailImgSet.push(this.form.mainImgUrl);
                    }
                    console.log(this.form);
                }
                this.open = true;
                this.title = '商品详情';
            });
        },
        // 状态字典翻译
        statusFormat(row, column) {
            return this.selectDictLabel(this.statusOptions, row.status);
        },
        /** 提交按钮 */
        submitForm: function() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateShop(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess('修改成功');
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        this.form.then(response => {
                            if (response.code === 200) {
                                this.msgSuccess('新增成功');
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除店铺编号为"' + ids + '"的数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function() {
                    return delShop(ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess('删除成功');
                })
                .catch(function() {});
        },
        /** 导出按钮操作 */
        handleExport() {
            if (this.ids.length < 1) {
                return;
            }
            exportGoods(this.ids).then(
                res => {
                    this.download(res.msg);

                    /* console.log("0000000");
         console.log(res);
         // 此处有个坑。这里用content保存文件流，最初是content=res，但下载的test.xls里的内容如下图1，
         // 检查了下才发现，后端对文件流做了一层封装，所以将content指向res.data即可
         // 另外，流的转储属于浅拷贝，所以此处的content转储仅仅是便于理解，并没有实际作用=_=
         const content = res.msg
         const blob = new Blob([content]) // 构造一个blob对象来处理数据
         const fileName = 'item.xlsx' // 导出文件名
         // 对于<a>标签，只有 Firefox 和 Chrome（内核） 支持 download 属性
         // IE10以上支持blob但是依然不支持download
         if ('download' in document.createElement('a')) { // 支持a标签download的浏览器
           const link = document.createElement('a') // 创建a标签
           link.download = fileName // a标签添加属性
           link.style.display = 'none'
           link.href = URL.createObjectURL(blob)
           document.body.appendChild(link)
           link.click() // 执行下载
           URL.revokeObjectURL(link.href) // 释放url
           document.body.removeChild(link) // 释放标签
         } else { // 其他浏览器
           navigator.msSaveBlob(blob, fileName)
         }*/
                },
                err => {
                    console.log(err);
                }
            );
            // .then(function() {
            //   return exportShop(queryParams);
            // })
            // .then(response => {
            //   this.download(response.msg);
            // })
            // .catch(function() {});
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.xian {
    width: 20px;
    height: 1px;
    margin: 18px 5px 0 0 !important;

    background: #333333;
    display: inline-block;
}
.goodsall .title {
    margin-bottom: 8px;
}
.goodsall .title_b label,
.goodsall .title_b div {
    font-size: 12px !important;
    color: #99a9bf;
}
.goodsall .title label {
    font-size: 16px;
    line-height: 16px;
    color: #333;
    font-weight: 400;
    margin: 10px 0;
}
.goodsall .el-table__expanded-cell {
    padding: 20px 50px 20px 90px !important;
}
.goodImages {
    overflow: hidden;
    margin-left: 60px;
    .imgTitle {
        text-align: right;
        vertical-align: middle;
        float: left;
        font-size: 14px;
        color: #606266;
        box-sizing: border-box;
        line-height: 36px;
        font-weight: 600;
        margin: 5px 0;
    }
    .imgTitle + div {
        overflow: hidden;
    }
    .goodImg {
        float: left;
        margin: 15px;
        width: 100px;
        height: 100px;
    }
}
</style>
